<template>
  <el-submenu
    v-if="menu.list && menu.list.length >= 1"
    :index="menu.menuId + ''"
    :popper-class="'site-sidebar--' + sidebarLayoutSkin + '-popper'">
    <template slot="title">
      <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
      <span>{{ menu.name }}</span>
    </template>
    <sub-menu
      v-for="item in menu.list"
      :key="item.menuId"
      :menu="item"
      :dynamicMenuRoutes="dynamicMenuRoutes">
    </sub-menu>
  </el-submenu>
  <el-menu-item v-else :index="menu.menuId + ''" @click="gotoRouteHandle(menu)">
    <icon-svg :name="menu.icon || ''" class="site-sidebar__menu-icon"></icon-svg>
    <span>{{ menu.name }}</span>
  </el-menu-item>
</template>

<script>
  import SubMenu from './main-sidebar-sub-menu'
  import { mapActions } from 'vuex';
  export default {
    name: 'sub-menu',
    props: {
      menu: {
        type: Object,
        required: true
      },
      dynamicMenuRoutes: {
        type: Array,
        required: true
      }
    },
    components: {
      SubMenu
    },
    computed: {
      sidebarLayoutSkin: {
        get () { return this.$store.state.common.sidebarLayoutSkin }
      }
    },
    methods: {
      ...mapActions('programs', ['getProgramInfo']),
      // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
      gotoRouteHandle (menu) {
        if(menu.name==='新建节目'){
          let params = {
            height: 1920,
            width: 1080,
            programName: '默认名称',
            value: 1920+'*'+1080
          }
          this.$http({
            url: this.$http.adornUrl(`/program/save`),
            method: 'post',
            data: this.$http.adornData({
              'id': undefined,
              'programName': params.programName,
              'fenbianlv': params.value
            })
          }).then(({data}) => {  
            if (data && data.code === 0) {
              this.getProgramInfo(data.data.id*1);
              this.$router.push({
                name: 'programs-edit',
                params: {
                  status: 'new',
                  id: data.data.id
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
        var route = this.dynamicMenuRoutes.filter(item => item.meta.menuId === menu.menuId)
        if (route.length >= 1) {
          this.$router.push({ name: route[0].name })
        }
      }
    }
  }
</script>
